package com.phx.serialjni;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LightingColorFilter;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.Rect;
import android.support.v4.view.ViewCompat;
import android.view.MotionEvent;
import android.view.TouchDelegate;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;


import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.view.View;
import android.graphics.Camera;
import android.widget.ImageView;

import java.lang.reflect.Method;

public class MeziView extends View {
    //定义相关变量,依次是妹子显示位置的X,Y坐标
    public float bitmapX;
    public float bitmapY;
    public float mStep,mRadius,mDrawRadius,mInitX,mInitY;
    public int mleft;
    public int mtop;
    public int mwidth;
    public int mheight;
    Paint mRevealPaint;
    public int mRes;
    public Bitmap mResBitmap;
    public int type;
    public int ho;
    public int lv;
    public  int la;
    public  int iconNum;
    static  public boolean fristFlag;
    MainActivityf mmainActivityf;
    Rect mRect;
    Bitmap dstbmp;
    Matrix matrix;
    static public Bitmap[] icon = new  Bitmap[10] ;
    static public Bitmap[] iconbj = new  Bitmap[10] ;
    static public Bitmap[] iconqj = new  Bitmap[10] ;
    public static boolean mDrawFinish,mDrawBack,touchFlag;
    private Camera camera;
    ImageView show_image;
    public MeziView(Context context) {
        super(context);
        //设置妹子的起始坐标
        bitmapX = 0;
        bitmapY = 200;
        mleft = 0;
        mtop = 0;
        mwidth = 50;
        mheight = 50;
        mResBitmap =  PageS.page[0].icon[0].iconbj ;
        mRes = R.drawable.b2_off;
        type = 0;
        ho= 255;
        lv=255;
        la=255;
    }

    //重写View类的onDraw()方法
    public Bitmap createImage(Rect mDestRect) {
        //由于直接new出来的view是不会走测量、布局、绘制的方法的，所以需要我们手动去调这些方法，不然生成的图片就是黑色的。
        int widthMeasureSpec = MeasureSpec.makeMeasureSpec(mDestRect.width(), MeasureSpec.EXACTLY);
        int heightMeasureSpec = MeasureSpec.makeMeasureSpec(mDestRect.height(), MeasureSpec.EXACTLY);
        measure(widthMeasureSpec, heightMeasureSpec);
        layout(0, 0, mDestRect.width(),mDestRect.height());
        Bitmap bitmap = Bitmap.createBitmap(mDestRect.width(), mDestRect.height(), Bitmap.Config.ARGB_8888);
        return bitmap;
    }
    public void repaint(){
        //使当前页面无效，这样可以调用onDraw（）函数重新绘画当前界面
        this.invalidate();
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //mRect=new Rect(0, 0, getMeasuredWidth(), getMeasuredHeight());
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
    public void  postOnAnimation11(){
        ViewCompat.postInvalidateOnAnimation(this);
    }

    /**
          * 翻转图片
          *
          * @param isLeftOrRight
          */
    private void roatePicture(boolean isLeftOrRight, Bitmap bitmap) {
        camera.save();
        Matrix matrix = new Matrix();
        // rotate
        if (isLeftOrRight) {
            camera.rotateY(180);


        } else {
            camera.rotateX(180);
        }
        camera.getMatrix(matrix);
        // 恢复到之前的初始状态。
        camera.restore();
        // 设置图像处理的中心点
        matrix.preTranslate(bitmap.getWidth() >> 1, bitmap.getHeight() >> 1);
        //    matrix.preSkew(10, 10);
        matrix.postTranslate(bitmap.getWidth(), bitmap.getHeight());
        // matrix.postSkew(skewX, skewY);
        // 直接setSkew()，则前面处理的rotate()、translate()等等都将无效。
        // matrix.setSkew(skewX, skewY);
        // 2.通过矩阵生成新图像(或直接作用于Canvas)
        Bitmap newBit = null,tempBitmap;
        try {
            // 经过矩阵转换后的图像宽高有可能不大于0，此时会抛出IllegalArgumentException
            newBit = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        } catch (IllegalArgumentException iae) {
            iae.printStackTrace();
        }
        if (newBit != null) {
//            show_image.mImageView.setLayout(false);
            show_image.setImageBitmap(newBit);
            tempBitmap = newBit;
        }
    }

    //@Override
    protected void onDraw(Canvas canvas) {
        //super.onDraw(canvas);
        if (mDrawFinish) {
            super.onDraw(canvas);
            touchFlag=false;

            return;
        }
//////////////////////////////////
        camera = new Camera();
        int  maxHeight = canvas.getHeight();
  /////////////////////////
        //创建,并且实例化Paint的对象
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        //根据图片生成位图对象
       //Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(), mRes);
        Bitmap bitmap = icon[0];
        if ((fristFlag==true)&&(iconNum<PageS.page[0].totalIconNum)){
           // iconNum++;

            bitmap =icon[iconNum++];
       }
        bitmap =  mResBitmap;

       // Resources rs =  MainActivityf.getApplicationContext().getResources();
         Rect mDestRect ;//= new Rect(mleft, mtop, mleft + mwidth, mtop + mheight);
       // MainActivityf mmainActivityf = new MainActivityf();
      //  boolean hasNavigationBar = mmainActivityf.checkDeviceHasNavigationBar();
      //  MeziView.getLocationOnScreen();
        //if(MainActivityf.checkDeviceHasNavigationBarFlag)
          //  if(MainActivityf.checkDeviceHasNavigationBar(MainActivityf.MainActivitycontext))
        if(maxHeight==MainActivityf.screenHeight){
            mDestRect = mRect;}
       else {
       //   mDestRect = mRect;
           mDestRect =new Rect(mRect.left ,mRect.top  * maxHeight/MainActivityf.screenHeight,
                   mRect.right, mRect.bottom * maxHeight/MainActivityf.screenHeight);
      }


        if(type == 1){
            paint.setColor(Color.TRANSPARENT);

            canvas.drawRect(mDestRect,paint);
         }
        else {//反馈
            paint.setColorFilter(new PorterDuffColorFilter(Color.rgb(ho,lv,la), PorterDuff.Mode.MULTIPLY));
           ///////////////////////////
            if(touchFlag==true) {
                mStep = 30;
                matrix = new Matrix();
                // 缩放原图
                matrix.postScale(1f, 1f);
                mDrawRadius = mDrawRadius + mStep;
                matrix.postRotate(-mDrawRadius);
                dstbmp = Bitmap.createBitmap(bitmap, 0,0, bitmap.getWidth(), bitmap.getHeight(),
                        matrix, true);
                if (mDrawRadius >(mRadius-mStep)){
                    mDrawRadius = 0;
                    canvas.drawBitmap(dstbmp, null, mDestRect, paint);
                       mDrawFinish = true;
                    return;
                }else {
                    canvas.drawBitmap(dstbmp, null, mDestRect, paint);
                    ViewCompat.postInvalidateOnAnimation(this);
                }
            }
            else////////////////*/
                {
                canvas.drawBitmap(bitmap, null, mDestRect, paint);
                    //  canvas.drawBitmap(icon[5], null, mDestRect, paint);
            }
        }
        //判断图片是否回收,木有回收的话强制收回图片
        if(bitmap!=null && bitmap.isRecycled()){
            bitmap.recycle();
        }
    }


}


